/**
 * 
 */
package TextEditor;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * Class encapsulating searching operations
 * @author yij_zhan
 */
public class FindAndReplace extends JDialog implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTextArea textArea;
	
    private JPanel replacePane = new JPanel();

	private JLabel findLabel = new JLabel(" Find What :");
	private JTextField findText = new JTextField(25);
	private JButton findBtn = new JButton(" Find Next ");

	private JLabel replaceLabel = new JLabel("Replace with:");
	private JTextField replaceText = new JTextField(25);
	private JButton replaceBtn = new JButton(" Replace ");
	private JButton replaceAllBtn = new JButton("Replace All");

	private JButton cancelBtn = new JButton("Cancel");

	JCheckBox matchCheckBox = new JCheckBox("Match case:");
	ButtonGroup bGroup = new ButtonGroup();
	JRadioButton upButton = new JRadioButton("Up");
	JRadioButton downButton = new JRadioButton("Down");

	JPanel directionPanel = new JPanel();
	JPanel panel1 = new JPanel();
	JPanel panel2 = new JPanel();
	JPanel panel3 = new JPanel();
	JPanel panel4 = new JPanel();
	
	private int head = 0 ; // tail = 0, current = 0;


	/**Constructor*/
	public FindAndReplace(JTextArea textArea) {
		
		this.textArea = textArea;
		
		bGroup.add(upButton);
		bGroup.add(downButton);		
		downButton.setSelected(true);
		
		directionPanel.setBorder(BorderFactory.createTitledBorder("Direction:"));
		directionPanel.add(upButton);
		directionPanel.add(downButton);

		panel1.add(findLabel);
		panel1.add(findText);
		panel1.add(findBtn);
		findBtn.addActionListener(this);

		panel2.add(replaceLabel);
		panel2.add(replaceText);
		panel2.add(panel4);

		panel3.add(matchCheckBox);
		panel3.add(directionPanel);
		panel3.add(cancelBtn);
		cancelBtn.addActionListener(this);
		
		panel4.setLayout(new GridLayout(2,1));
		panel4.add(replaceBtn);
		replaceBtn.addActionListener(this);
		panel4.add(replaceAllBtn);
		replaceAllBtn.addActionListener(this);

		replacePane.add(panel1);
		replacePane.add(panel2);
		replacePane.add(panel3);

		setSize(500,220);
		setResizable(false);
		setModal(false);
		setLocation(380,280);

		setContentPane(replacePane);
		
        setVisible(true);

	}

	/**reuse by the search bar.*/
	private void find(String pattern, boolean isMatch,boolean isDown){
	
		int index = 0;
		textArea.requestFocusInWindow();
		
		String contents = textArea.getText();
		if (contents == null ) return;
		if (isMatch != true){
			pattern = pattern.toLowerCase();
			contents = contents.toLowerCase();
		}
		index = contents.indexOf(pattern, head);		
		if (index != -1){
			
			this.textArea.select(index, index + pattern.length());
			head = index + pattern.length()+ 1;
		}
		else {
			head = 0;	
			JOptionPane.showMessageDialog(textArea,"End of file, " +pattern +" is not find!"  ,"Information",JOptionPane.INFORMATION_MESSAGE);
		}
		
		
	}
		


	@Override
	public void actionPerformed(ActionEvent event) {
		
//		boolean isMatchCase = matchCheckBox.isSelected();
//		boolean isDownWard = downButton.isSelected(); 

		Object source = event.getSource();

		
		if(source == findBtn){
			
			String pattern = findText.getText().trim();
			if (pattern == null) return;
			
			//Default TextArea is here.
			find(pattern,matchCheckBox.isSelected(),downButton.isSelected());		
			
		}
		else if (source == replaceBtn ) {
			String pattern = findText.getText().trim();
			String replacePattern = replaceText.getText().trim();
			if (pattern == null) return;
			if (replacePattern == null) replacePattern = "";
	 		JOptionPane.showMessageDialog(this, "Current Function is disabled.", "Information", 
					JOptionPane.INFORMATION_MESSAGE);
			
		}
		else if (source == replaceAllBtn){
			// add a boolean parameter to do replace all.
	 		JOptionPane.showMessageDialog(this, "Current Function is disabled.", "Information", 
					JOptionPane.INFORMATION_MESSAGE);
		}
		else {
			//default cancel event handles. close and release all resource.
			this.setVisible(false);
			this.dispose();
		}
		
	}

}

